/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-02-07 18:00:22
 */
import { MiddlewareConsumer, Module, RequestMethod } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ThrottlerModule, ThrottlerGuard } from '@nestjs/throttler';
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import { I18nModule, QueryResolver } from 'nestjs-i18n';
import * as path from 'path';

import configuration from '../config/configuration';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { DefaultDataService } from './DefaultDataService.service';
import { HomeModule } from './apiModule/home';
import { AdminModule } from './apiModule/admin';
import { AdminUserModule } from './modules/admin/admin-user/admin-user.module';
import { ScheduleModule } from '@nestjs/schedule';
import { ResetService } from './modules/reset/reset.service';
import { LoggerMiddleware } from './common/log4js/logger.middleware';
import { HttpExceptionFilter } from './common/http-exception.filter';
import { MenuModule } from './modules/admin/menu/menu.module';
// 环境变量加载
const envFilePath = ['env/.env'];
if (process.env.NODE_ENV) {
  envFilePath.unshift(`env/.env.${process.env.NODE_ENV}`);
}
@Module({
  imports: [
    // 配置国际化模块
    I18nModule.forRoot({
      // 设置默认语言为英语
      fallbackLanguage: 'en',
      // 加载器选项
      loaderOptions: {
        // 指定国际化文件路径
        path: path.join(__dirname, '..', 'i18n'), // 调整路径
        // 监视文件变化
        watch: true,
      },
      // 配置解析器，支持通过查询参数 'lang' 或 'l' 来指定语言
      resolvers: [new QueryResolver(['lang', 'l'])],
    }),

    /** 定时 */
    ScheduleModule.forRoot(),
    ConfigModule.forRoot({
      load: [configuration],
      envFilePath,
      isGlobal: true,         // 使其全局可用
    }),
    // 限度客户端在规定的时间内申请次数
    ThrottlerModule.forRoot({
      ttl: 60, //1分钟
      limit: 60, //申请60次
    }),
    // 使用 ConfigService 动态配置 TypeOrm
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        type: 'mysql',
        host: configService.get<string>('DB_HOST'),
        port: configService.get<number>('DB_PORT'),
        username: configService.get<string>('DB_USERNAME'),
        password: configService.get<string>('DB_PASSWORD'),
        database: configService.get<string>('DB_DATABASE'),
        synchronize: configService.get<boolean>('DB_SYNCHRONIZE'),
        logging: configService.get<boolean>('DB_LOGGING'),
        autoLoadEntities: true,
        cache: true,
      }),
    }),
    ...AdminModule,
    ...HomeModule,

  ],
  controllers: [AppController],
  providers: [
    AppService,
    MenuModule,
    AdminUserModule,
    DefaultDataService,
    ResetService,
    {
      provide: APP_FILTER,
      useClass: HttpExceptionFilter,
    },
    {
      provide: APP_GUARD,
      useClass: ThrottlerGuard,
    },
  ],
})
export class AppModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(LoggerMiddleware)
      .forRoutes({ path: '*', method: RequestMethod.ALL });
  }
}
